package api;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘柄华
 * Date: 2022-06-21
 * Time: 19:57
 */

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");

        // 处理注销的逻辑  删掉 session 会话即可
        HttpSession session = req.getSession(false);
        if (session == null){
            // 获取不到 session, 即当前用户未登录
            String html = "当前尚未登录!";
            resp.getWriter().write(html);
            return;
        }
        session.removeAttribute("user");
        // 注销后 重定向到登录页面
        resp.sendRedirect("login.html");
    }
}
